package cn.ac.siom.query.action;

import cn.ac.siom.query.service.ITab_articleService;
import cn.ac.siom.query.util.action.DefaultAction;
import cn.ac.siom.query.vo.Tab_article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/home/*")
public class Tab_articleAction extends DefaultAction {
    @Resource
    private ITab_articleService tab_articleService;

    /**
     * 激光通讯没用此接口
     */
    @RequestMapping(value = "tab_article_list")
    public void tab_article_Conference_list(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        try {
            insertAccessinfo(request,"");
            super.printObjectToList(response, "allTab_article", this.tab_articleService.list());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @ResponseBody
    @RequestMapping(value="findNO_Tab_article", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<Tab_article> findNO(HttpServletRequest request, Integer i_id) throws SQLException {
        insertAccessinfo(request,"");
        return (List<Tab_article>) this.tab_articleService.list_NO(i_id);
    }

    /**
     * 激光通讯没用此接口
     */
    @ResponseBody
    @RequestMapping(value="findPage_Tab_article", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String, Object> findPage(HttpServletRequest request, Integer rownum, Integer page_id) throws SQLException {
        insertAccessinfo(request,"");
        List<Tab_article>  tab_articles = (List<Tab_article>) this.tab_articleService.list_page(rownum,page_id);
        Integer cont=this.tab_articleService.list_count();
        Map<String,Object> map = new HashMap<>();
        map.put("tab_articles",tab_articles);
        map.put("cont",cont);
        map.put("page_id",page_id);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="findPhtoto_http_Tab_article", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<Map<String,String>> findPhtoto_http(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        insertAccessinfo(request,"核心期刊");
        List<Tab_article>  tab_articles =  this.tab_articleService.list_http_photo();
        List<Map<String ,String>> list=new ArrayList<Map<String ,String>>();

        for(Tab_article tab_article:tab_articles) {
            Map<String ,String> map = new HashMap<String ,String>();
            String http=tab_article.getField07();
            String name=tab_article.getField03();
            String photo = "http://www.optics.org.cn/web/QKfm/"+tab_article.getRecordNO()+".jpg";
            map.put("photo",photo);
            map.put("http",http);
            map.put("name",name);
            list.add(map);
        }
        return list;
    }


    @Override
    public String getText() {
        return null;
    }
}
